package com.xujf.demo.newsChannel

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.helper.ItemTouchHelper
import android.widget.Toast
import com.xujf.demo.R
import com.xujf.demo.base.BaseSimpleActivity
import kotlinx.android.synthetic.main.activity_news_channel.*

class NewsChannelActivity : BaseSimpleActivity() {

    private lateinit var adapter: NewsChannelAdapter

    override fun setUpUI(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_news_channel)
        getBaseToolBar().title = "拖拽排序"
        initData()
    }

    private fun initData() {
        val enableItems = ItemsDao().enableData
        val disableItems = ItemsDao().disableData

        val manager = GridLayoutManager(this, 4)
        rv.layoutManager = manager

        val callback = ItemDragHelperCallback()
        val helper = ItemTouchHelper(callback)
        helper.attachToRecyclerView(rv)

        adapter = NewsChannelAdapter(this, helper, enableItems, disableItems)
        manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                val viewType = adapter.getItemViewType(position)
                return if (viewType == NewsChannelAdapter.TYPE_MY || viewType == NewsChannelAdapter.TYPE_OTHER) 1 else 4
            }
        }
        rv.adapter = adapter

        adapter.setOnMyChannelItemClickListener { v, position -> Toast.makeText(this@NewsChannelActivity, enableItems[position].channelName + position, Toast.LENGTH_SHORT).show() }
    }
}
